polytype
A Hindley-Milner polymorphic typing system. Implements type inference via unification.
Usage
[]
= "6.2"
polytype
provides the
TypeSchema
and
Type
enums, the
Context
struct, and the
tp!
and
ptp!
macros which
help to concisely create types and type schemas.
Unification:
let mut ctx = default;
// t1: list(int → α) ; t2: list(β → bool)
let t1 = tp!;
let t2 = tp!;
ctx.unify.expect;
let t1 = t1.apply;
let t2 = t2.apply;
assert_eq!; // list(int → bool)
Apply a type context:
let mut ctx = default;
// assign t0 to int
ctx.extend;
let t = tp!;
assert_eq!;
let t = t.apply;
assert_eq!;
Instantiate a TypeSchema
:
let mut ctx = default;
// ∀α. list(α)
let schema = ptp!;
// They instantiate to new fresh type variables
let t1 = schema.instantiate;
let t2 = schema.instantiate;
assert_eq!;
assert_eq!;
See the documentation for more details.
Features
By default polytype
includes a type parser that can be invoked with Type::parse
.
This can be disabled with default-features = false
.